<template>
  <div>
    <el-card shadow="never" class="aui-card--fill">
      <vxe-toolbar :setting="{immediate:true}">
        <template v-slot:buttons>
          <el-button v-if="isSuperAdmin||$hasPermission('sys:dicedata:save')" type="primary" @click="addOrUpdateHandle()">
            {{$t('add') }}
          </el-button>
          <el-button v-if="isSuperAdmin||$hasPermission('sys:dicedata:delete')" type="danger" @click="deleteHandle()">
            {{$t('deleteBatch') }}
          </el-button>
        </template>
        <template v-slot:tools>
          <el-button icon="el-icon-refresh" @click="getDataList()" circle></el-button>
          <!-- <el-button icon="el-icon-download" @click="$refs.xTable.openExport()" circle></el-button> -->
        </template>
      </vxe-toolbar>
      <vxe-table :loading="dataListLoading" ref="xTable" auto-resize :data="dataList" class="vxe-table-element"
        @select-change="dataListSelectionChangeHandleVex" @select-all="dataListSelectionAllChangeHandleVex">
        <vxe-table-column type="checkbox" width="50" align="center"></vxe-table-column>
        <vxe-table-column field="dictType" title="dict.dictName"></vxe-table-column>
        <vxe-table-column field="dictLabel" title="dict.label"></vxe-table-column>
        <vxe-table-column field="dictValue" title="dict.dictValue"></vxe-table-column>
        <vxe-table-column field="sort" title="dict.sort" width="60" align="center"></vxe-table-column>
        <vxe-table-column field="remark" title="dict.remark"></vxe-table-column>
        <vxe-table-column field="status" title="dict.state" width="80" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 0" size="small" type="danger">{{ $t('dict.status0') }}</el-tag>
            <el-tag v-else size="small" type="success">{{ $t('dict.status1') }}</el-tag>
          </template>
        </vxe-table-column>
        <vxe-table-column title="handle" width="120">
          <template slot-scope="scope" align="center">
            <el-button v-if="isSuperAdmin||$hasPermission('sys:dicedata:update')" type="text" icon="el-icon-edit" size="small"
              @click="addOrUpdateHandle(scope.row.id)">{{
              $t('update') }}
            </el-button>
            <el-button v-if="isSuperAdmin||$hasPermission('sys:dicedata:delete')" type="text" icon="el-icon-delete"
              size="small" @click="deleteHandle(scope.row.id)">{{
              $t('delete') }}
            </el-button>
          </template>
        </vxe-table-column>
      </vxe-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './dictdata-add-or-update'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        activatedIsNeed: true,
        getDataListURL: '/sys/sysdictdata/page',
        getDataListIsPage: true,
        deleteURL: '/sys/sysdictdata',
        deleteIsBatch: true
      },
      dataForm: {
        id: '',
        pid: this.$route.params.dictType.id
      },
      dictType: {}
    }
  },
  computed: {
    isSuperAdmin () {
      return this.$store.state.user.superAdmin === 1
    }
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id
        this.$refs.addOrUpdate.dataForm.pid = this.dictType.id
        this.$refs.addOrUpdate.dataForm.dictType = this.dictType.dictType
        this.$refs.addOrUpdate.dataForm.dictTypeName = this.dictType.dictName
        this.$refs.addOrUpdate.dictType = this.dictType
        this.$refs.addOrUpdate.init()
      })
    }
  },
  activated () {
    // 通过路由参数pid, 控制列表请求操作
    if (this.$route.params.dictType) {
      this.dictType = this.$route.params.dictType
      this.dataForm.pid = this.dictType.id
      this.getDataList()
    } else {
      console.log('路由调转')
    }
  },
  components: {
    AddOrUpdate
  }
}

</script>
